package com.donzell.web.controller.admin;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.donzell.dao.AdminUserDao;
import com.donzell.dao.RoleDao;
import com.donzell.model.EcsAdminUser;
import com.donzell.model.EcsRole;

@RestController
@RequestMapping("/init")
public class InitController {
	@Autowired
	private AdminUserDao adminUserDao;
	
	@Autowired
	private RoleDao roleDao;
	
	@RequestMapping("/")
	public String init(){
		initRole();
		initAdminUser();
		return "初始化完成";
	}
	
	private void initRole(){
		EcsRole superManager = new EcsRole("超级管理员","1,2,3,4,5,6,7,101,202","拥有至高无上的权利");
		roleDao.save(superManager);
		EcsRole normalManager = new EcsRole("总编","1,2,3,4,5,6,7,101,202","具有添加、审核、发布、删除内容的权限");
		roleDao.save(normalManager);
		EcsRole normarUser = new EcsRole("栏目主辑","1,2,3,4","只对所在栏目具有添加、审核、发布、删除内容的权限");
		roleDao.save(normarUser);
		EcsRole normarUser1 = new EcsRole("栏目编辑","1,2,3,4","只对所在栏目具有添加、删除草稿等权利。");
		roleDao.save(normarUser1);
	}
	
	private void initAdminUser(){
		EcsAdminUser adminUser1 = new EcsAdminUser();
		adminUser1.setUserName("test");
		adminUser1.setPassword("21232F297A57A5A743894A0E4A801FC3");
		adminUser1.setPhone("18140180205");
		adminUser1.setEmail("17273333@qq.com");
		adminUser1.setRoleId(Short.valueOf("2"));
		adminUser1.setIsUse(Short.valueOf("0"));
		adminUser1.setAddTime(new Date(1990,2,7));
		adminUser1.setLastLogin(new Date());
		adminUserDao.save(adminUser1);
		
		EcsAdminUser adminUser = new EcsAdminUser();
		adminUser.setUserName("admin");
		adminUser.setPassword("21232F297A57A5A743894A0E4A801FC3");
		adminUser.setPhone("18140180207");
		adminUser.setEmail("172733456@qq.com");
		adminUser.setRoleId(Short.valueOf("1"));
		adminUser.setIsUse(Short.valueOf("1"));
		adminUser.setAddTime(new Date());
		adminUser.setLastLogin(new Date());
		adminUserDao.save(adminUser);
		
		ArrayList<EcsAdminUser> adminUserList = new ArrayList();
		for (int i = 0; i < 10; i++) {
			EcsAdminUser adminUserTemp = new EcsAdminUser();
			adminUserTemp.setUserName("admin"+i);
			adminUserTemp.setPassword("21232F297A57A5A743894A0E4A801FC3");
			adminUserTemp.setPhone("18108103363");
			adminUserTemp.setEmail("17273@qq.com");
			adminUserTemp.setRoleId(Short.valueOf("1"));
			adminUserTemp.setIsUse(Short.valueOf("1"));
			adminUserTemp.setAddTime(new Date());
			adminUserTemp.setLastLogin(new Date());
			adminUserList.add(adminUserTemp);
		}
		adminUserDao.save(adminUserList);
	}
}
